package com.party.service.vote.impl;

import com.party.common.Page;
import com.party.mapper.VoPtionMapper;
import com.party.model.VoPtion;
import com.party.model.VoPtionExample;
import com.party.service.vote.VoPtionService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class VoPtionServiceImpl implements VoPtionService {

    @Resource
    VoPtionMapper voPtionMapper;

    @Override
    public int addVoPtion(VoPtion voPtion) {
        int num = voPtionMapper.insertSelective(voPtion);
        return num;
    }

    @Override
    public int deleteVoPtionByPrimaryKey(Integer id) {
        int num = voPtionMapper.deleteByPrimaryKey(id);
        return num;
    }

    public int deleteVoPtionByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += voPtionMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public VoPtion getVoPtionByPrimaryKey(Integer id) {
        VoPtion voPtion = voPtionMapper.selectByPrimaryKey(id);
        return voPtion;
    }

    @Override
    public int updateVoPtionByPrimaryKey(VoPtion VoPtion) {
        int num = voPtionMapper.updateByPrimaryKeySelective(VoPtion);
        return num;
    }

    @Override
    public  List<VoPtion> getAllVoPtionByVoteId(Integer voteid) {
        VoPtionExample voPtionExample = new VoPtionExample();
        VoPtionExample.Criteria criteria=voPtionExample.createCriteria();
        criteria.andVoteidEqualTo(voteid);
        List<VoPtion> listVoPtion=voPtionMapper.selectByExample(voPtionExample);
        return listVoPtion;
    }

    @Override
    public int updateVoPtionVotesByIds(String[] values) {
        int num = 0;
        if (values != null&&!"".equals(values)) {
            for (String optionid : values) {
                if(!"".equals(optionid)){
                    VoPtion voPtion=voPtionMapper.selectByPrimaryKey(Integer.parseInt(optionid));
                    voPtion.setVotes(voPtion.getVotes()+1);
                    num += voPtionMapper.updateByPrimaryKeySelective(voPtion);
                }
            }
        }
        return num;
    }

    @Override
    public Page getVoPtionList(VoPtion voPtion, int pageSize, int pageNumber) {
        /**
         * 可以添加查询条件
         */
        int count = voPtionMapper.countByVoPtion(voPtion);

        //获取总的记录数量
        //获取指定的页码数据
        voPtion.setBeginNum((pageNumber - 1) * pageSize);
        voPtion.setEndNum(pageSize);
        List<VoPtion> list = voPtionMapper.getListPage(voPtion);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
